//
//  FKHomeCategoryCell.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/16.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKHomeCategoryCell.h"
#import "FKHomeCategoryItemCell.h"
static NSString *const ID = @"FKHomeCategoryItemCell";

@interface FKHomeCategoryCell ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *datas;

@end

@implementation FKHomeCategoryCell

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (void)setupSubviews
{
    [super setupSubviews];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat itemW = fkScreenW / 4.f;
    CGFloat itemH = fk_adjustW(82);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(itemW, itemH);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.contentView addSubview:collectionView];
    self.collectionView = collectionView;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[FKHomeCategoryItemCell class] forCellWithReuseIdentifier:ID];
    
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKHomeCategoryItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.data = self.datas[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(homeCategoryCell:didSelectedItem:)]) {
        NSDictionary *data = self.datas[indexPath.item];
        [self.delegate homeCategoryCell:self didSelectedItem:data];
    }
}

#pragma mark - UICollectionViewDelegate
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
////    NSDictionary *group = self.datas[indexPath.section];
////    NSArray *items = group[@"items"];
////    NSDictionary *item = items[indexPath.item];
////    return [item[@"cellSize"] CGSizeValue];
//}

- (void)setData:(id)data
{
    [super setData:data];
    
    self.datas = data;
    [self.collectionView reloadData];
}

@end
